package cn.shop.core.controller;

import cn.shop.core.bean.UserInfo;
import cn.shop.core.service.UserService;
import cn.shop.utils.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2016/8/25.
 */
@Service

public class UserController extends BaseController {
    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/")
//    public String index() {
//        return "index";
//    }

    /**
     * 手机号码检测
     *
     * @param user
     * @return
     */
    @RequestMapping("user/userajax.do")
    public
    @ResponseBody
    String userAjax(UserInfo user) {
        System.out.println(user);
        UserInfo puser = userService.getUserInfo(user);
        System.out.println(puser);
        if (puser != null) {
            System.out.println("puser不可用");
            return "手机号码已经存在，请重新输入";
        } else {
            System.out.println("puser可用");
            return "恭喜您，手机号码可以使用!";
        }
    }

    @RequestMapping("back/login.do")
    public String login() {
        return "main/index";
    }

    @RequestMapping("back/bootm.do")
    public String bootm() {
        return "main/bootm";
    }

    @RequestMapping("/user/list.do")
    public String list(UserInfo user, Model model, HttpServletRequest request) {
        if (user != null) {
            this.initPage(request);
            user.setStart(this.getPageNo());
            user.setLength(PAGE_NUM_BIG);
        }
        System.out.println("进入网页传入的搜索值：" + user.getUserName());
        //获取总记录数，查询条件为用户名，可为空
        List<UserInfo> list = userService.getList(user);
        model.addAttribute("list", list);
        model.addAttribute("total", userService.getCount(user));
        return "userinfo/userinfo_list";
    }

    @RequestMapping("user/loadadd.do")
    public String addUser() {
        return "userinfo/userinfo_add";
    }

    @RequestMapping("user/add.do")
    public String add(UserInfo user, Model model) {
        String info = "操作失败";
        try {
            userService.addUser(user);
            info = "操作成功";

        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("info", info);
        return "userinfo/userinfo_info";
    }

    /**
     * 加载修改页面
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("user/loadupdate.do")
    public String loadupdate(UserInfo user, Model model){
        model.addAttribute("user",userService.getUserInfo(user));
        return "userinfo/userinfo_update";
    }

    /**
     * 修改用户信息
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("user/update.do")
    public String update(UserInfo user, Model model){
        String info ="操作失败";
        try {
            userService.update(user);
            info = "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("info",info);
        return "userinfo/userinfo_info";
    }

    @RequestMapping("user/delete.do")
    public String delete(UserInfo user, Model model){
        String info ="操作失败";
        try {
            userService.deleteUser(user);
            info = "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("info",info);
        return "userinfo/userinfo_info";
    }
}
